package org.erepublik.company.controllers;

import java.util.Arrays;

import org.erepublik.company.PriceIndex;
import org.erepublik.company.comparators.FoodComparatorByWellness;
import org.erepublik.company.searchCriteria.EFoodSearchCriteria;
import org.erepublik.company.searchCriteria.FoodPriceSearchCriteria;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/foodPricesHistory.html")
public class FoodPricesHistoryController extends AbstractController {

    
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap model) {
        fillModelMap(model);
        PriceIndex[] prices = countryDataService.getPricesIndex("food", EFoodSearchCriteria.WELLNESS);
        Arrays.sort(prices);
        model.addAttribute("prices", prices);
        model.addAttribute("atribute", new FoodComparatorByWellness());
        model.addAttribute("priceFoodSearchCriteria", new FoodPriceSearchCriteria());
        return "foodPricesHistory";
    }
    
    
    @RequestMapping(method = RequestMethod.POST)
    public String showList(ModelMap model, @ModelAttribute("priceFoodSearchCriteria") FoodPriceSearchCriteria criteria) {
        fillModelMap(model);
        PriceIndex[] prices = countryDataService.getPricesIndex("food", EFoodSearchCriteria.WELLNESS);
        Arrays.sort(prices);
        
        model.addAttribute("prices", prices);
        model.addAttribute("atribute", new FoodComparatorByWellness());
        model.addAttribute("priceFoodSearchCriteria", criteria);
        return "foodPricesHistory";
    }
    

    
}
